package com.ynet.middleground.mobile.approve.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Throwables;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.mobile.approve.commom.ResponseResult;
import com.ynet.middleground.mobile.approve.constant.ErrorMessageConstant;
import com.ynet.middleground.mobile.approve.request.*;
import com.ynet.middleground.mobile.approve.response.*;
import com.ynet.middleground.mobile.approve.service.ApproveCenterService;
import com.ynet.middleground.mobile.approve.service.UserCenterService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName DetailPageController
 * @Description 移动审批详情页调用API Controller
 * @Author liwq
 * @Date 2021/3/23 15:43
 * @Version 1.0
 */
@RestController
@RequestMapping("/")
@Api(value = "详情页相关服务", tags = "详情页相关服务")
public class DetailPageController {

    @Autowired
    ApproveCenterService approveCenterService;

    @Autowired
    UserCenterService userCenterService;

    /**
     * 同意页面-查询审批人
     *
     * @param req
     * @return
     */
    @PostMapping("/organization/employee")
    @ApiOperation(value = "同意页面-查询审批人", notes = "同意页面-查询审批人")
    public ResponseResult<List<ApprovePersonDTO>> getApproveEmployees(@RequestBody BaseTaskReq req) {
        ResponseResult<List<ApprovePersonDTO>> responseResult = new ResponseResult<>();
        List<ApprovePersonDTO> list = null;
        try {
            list = userCenterService.getApproveEmployees(req);
        } catch (BusinessException e) {
            responseResult.setError(e.getCode(), e.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "同意页面-查询审批人服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            responseResult.setError(ErrorMessageConstant.ECUC0019_CODE, ErrorMessageConstant.ECUC0019_MESSAGE);
        }
        responseResult.setData(list);
        return responseResult;
    }

    /**
     * 获取流程审批记录
     *
     * @param req
     * @return
     */
    @PostMapping("task/comment/list")
    @ApiOperation(value = "获取流程审批记录", notes = "获取流程审批记录")
    public ResponseResult<ApproveRecordDTO> getApproveRecords(@RequestBody @Valid ApproveRecordsReq req) {
        ResponseResult<ApproveRecordDTO> responseResult = new ResponseResult<>();
        try {
            ApproveRecordDTO approveRecordDTO = approveCenterService.getApproveRecords(req);
            responseResult.setData(approveRecordDTO);
        } catch (BusinessException e) {
            responseResult.setError(e.getCode(), e.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "获取流程审批记录服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            responseResult.setError(ErrorMessageConstant.ECUC0008_CODE, ErrorMessageConstant.ECUC0008_MESSAGE);
        }
        return responseResult;
    }

    /**
     * 获取审批流程图
     *
     * @param req
     * @return 图片Base64格式
     */
    @PostMapping("images")
    @ApiOperation(value = "获取审批流程图", notes = "获取审批流程图")
    public ResponseResult<ProcessBase64ImageDTO> getBase64Image(@RequestBody @Valid GetBase64ImageReq req) {
        ResponseResult<ProcessBase64ImageDTO> serviceResult = new ResponseResult<>();
        try {
            ProcessBase64ImageDTO imageDTOS = approveCenterService.getBase64Image(req);
            serviceResult.setData(imageDTOS);
        } catch (BusinessException e) {
            serviceResult.setError(e.getCode(), e.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "获取审批流程图服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError(ErrorMessageConstant.ECUC0003_CODE, ErrorMessageConstant.ECUC0003_MESSAGE);
        }
        return serviceResult;
    }

    /**
     * 查询任务详情信息
     *
     * @param req 查询任务请求信息
     * @return 任务详情信息
     */
    @PostMapping("/task/detail")
    @ApiOperation(value = "查询任务详情信息", notes = "查询任务详情信息")
    public ResponseResult<TaskDetailDTO> getTaskDetail(@RequestBody @Valid QueryTaskReq req) {
        ResponseResult<TaskDetailDTO> responseResult = new ResponseResult<>();
        try {
            TaskDetailDTO detail =
                approveCenterService.getTaskDetailById(req.getTaskId(), req.getUserId(), req.getDingtalkUserId());
            responseResult.setData(detail);
        } catch (BusinessException e) {
            responseResult.setError(e.getCode(), e.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "移动审批查询任务详情服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            responseResult.setError("ECMA0000", "服务异常，请联系系统管理员");
        }
        return responseResult;
    }

    /**
     * 查询任务是否已处理完成
     *
     * @param req 基本任务请求信息
     * @return
     */
    @PostMapping("/task/result")
    @ApiOperation(value = "查询任务是否已处理完成", notes = "查询任务是否已处理完成")
    public ResponseResult<TaskResultDTO> getTaskResult(@RequestBody @Valid BaseTaskReq req) {
        ResponseResult<TaskResultDTO> responseResult = new ResponseResult<>();
        try {
            responseResult.setData(approveCenterService.getTaskResultForChannel(req.getTaskId()));
        } catch (BusinessException e) {
            responseResult.setError(e.getCode(), e.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "移动审批领取服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            responseResult.setError("ECMA0000", "服务异常，请联系系统管理员");
        }
        return responseResult;
    }

    /**
     * 领取任务
     *
     * @param req 基本任务请求信息
     * @return
     */
    @PostMapping("/task/claim")
    @ApiOperation(value = "领取任务", notes = "领取任务")
    public ResponseResult<Object> claim(@RequestBody @Valid BaseTaskReq req) {
        ResponseResult<Object> responseResult = new ResponseResult<>();
        try {
            approveCenterService.claim(req.getTaskId(), req.getUserId());
        } catch (BusinessException e) {
            responseResult.setError(e.getCode(), e.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "移动审批领取服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            responseResult.setError("ECMA0000", "服务异常，请联系系统管理员");
        }
        return responseResult;
    }

    /**
     * 取消领取任务
     *
     * @param req 基本任务请求信息
     * @return
     */
    @PostMapping("/task/unclaim")
    @ApiOperation(value = "取消领取任务", notes = "取消领取任务")
    public ResponseResult<Object> unclaim(@RequestBody @Valid BaseTaskReq req) {
        ResponseResult<Object> responseResult = new ResponseResult<>();
        try {
            approveCenterService.unclaim(req.getTaskId(), req.getUserId());
        } catch (BusinessException e) {
            responseResult.setError(e.getCode(), e.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "移动审批取消领取服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            responseResult.setError("ECMA0000", "服务异常，请联系系统管理员");
        }
        return responseResult;
    }

    /**
     * 移动端触发退回服务处理
     *
     * @param req 退回服务请求信息
     * @return
     */
    @PostMapping("/task/return")
    @ApiOperation(value = "退回服务处理", notes = "退回服务处理")
    public ResponseResult<Object> recallBackToStartUserTask(@RequestBody @Valid RecallBackTaskReq req) {
        ResponseResult<Object> responseResult = new ResponseResult<>();
        try {
            approveCenterService.recallBackToStartUserTask(req);
        } catch (BusinessException e) {
            responseResult.setError(e.getCode(), e.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "移动审批退回服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            responseResult.setError("ECMA0000", "服务异常，请联系系统管理员");
        }
        return responseResult;
    }

    /**
     * 提交审核任务
     *
     * @param req 退回服务请求信息
     * @return
     */
    @PostMapping("/task/complete")
    @ApiOperation(value = "提交审核任务", notes = "提交审核任务")
    public ResponseResult<Object> complete(@RequestBody @Valid CompleteTaskReq req) {
        ResponseResult<Object> responseResult = new ResponseResult<>();
        try {
            approveCenterService.completeTask(req);
        } catch (BusinessException e) {
            responseResult.setError(e.getCode(), e.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "移动审批同意服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            responseResult.setError("ECMA0000", "服务异常，请联系系统管理员");
        }
        return responseResult;
    }

}
